package com.funny.model;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.slim3.datastore.Attribute;
import org.slim3.datastore.Datastore;
import org.slim3.datastore.Model;
import org.slim3.datastore.json.Default;
import org.slim3.datastore.json.Json;
import org.slim3.datastore.json.JsonCoder;
import org.slim3.datastore.json.JsonReader;
import org.slim3.datastore.json.JsonWriter;

import com.google.appengine.api.datastore.Key;

/**
 * @author nvhienict
 */
@Model(schemaVersion = 1)
public class StoryModel implements Serializable {

	/**
	 * implementation of {@link JsonCoder} for pkey of this entity.
	 * <p>NOTICE: provide original {@link JsonCoder} because original coder behave NON-SECURE.</p>
	 */
	public static class KeyCoder extends Default implements JsonCoder {

		@Override
		public void encode(JsonWriter writer, Key value) {
			writer.writeString(value.getName());
		}

		@Override
		public Key decode(JsonReader reader, Key defaultValue) {
			String text = reader.read();
			if (StringUtils.isNotEmpty(text)) {
				return Datastore.createKey(StoryModel.class, text);
			}
			return defaultValue;
		}
	}


	private static final long serialVersionUID = 1L;

	@Attribute(primaryKey = true, json = @Json(coder = KeyCoder.class))
	private Key key;

	@Attribute(version = true)
	private Long version;

	@Attribute
	private String title;

	@Attribute(lob = true)
	private String content;

	@Attribute
	private String category;

	@Attribute
	private Long view;


	/**
	 * Returns the key.
	 *
	 * @return the key
	 */
	public Key getKey() {
		return key;
	}

	/**
	 * Sets the key.
	 *
	 * @param key
	 *            the key
	 */
	public void setKey(Key key) {
		this.key = key;
	}

	/**
	 * Returns the version.
	 *
	 * @return the version
	 */
	public Long getVersion() {
		return version;
	}

	/**
	 * Sets the version.
	 *
	 * @param version
	 *            the version
	 */
	public void setVersion(Long version) {
		this.version = version;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((key == null) ? 0 : key.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		StoryModel other = (StoryModel) obj;
		if (key == null) {
			if (other.key != null) {
				return false;
			}
		} else if (!key.equals(other.key)) {
			return false;
		}
		return true;
	}

	/**
	 * @param title the title to set
	 * @category accessor
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the title
	 * @category accessor
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param content the content to set
	 * @category accessor
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * @return the content
	 * @category accessor
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @param category the category to set
	 * @category accessor
	 */
	public void setCategory(String category) {
		this.category = category;
	}

	/**
	 * @return the category
	 * @category accessor
	 */
	public String getCategory() {
		return category;
	}

	/**
	 * @param view the view to set
	 * @category accessor
	 */
	public void setView(Long view) {
		this.view = view;
	}

	/**
	 * @return the view
	 * @category accessor
	 */
	public Long getView() {
		return view;
	}
}
